module Aio::Base::Toolkit::IPAddr
	class << self

		# 子网掩码从点分十进制转换为数值
		# 255.255.255.0 => 24
		def mask_dotted_to_i(mask)
			arr = mask.split(".")
			binary = ""
			arr.each do |m|
				binary << m.to_i.to_s(2)
			end
			return binary.scan(/1/).size
		end

		# 子网掩码从十进制到点分十进制
		# 24 => 255.255.255.0
		def mask_i_to_dotted(int)
			int = int.to_i
			arr = [nil, nil, nil, nil]
			binary = (2 ** int - 1) << (32 - int)
			4.times do |t|
				arr[3-t] = (binary & 255).to_s
				binary = binary >> 8
			end
			return arr.join(".")
		end
	end
end
